package com.hafu.view.ShangPin;
import com.hafu.model.SpInfo;
import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;
import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class ManageShangPinFrm extends JFrame {
    private SpInfoService spInfoService = new SpInfoService();

    public ManageShangPinFrm() {
        initComponents();
        fillTable();
    }

    private void fillTable() {
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        ArrayList<SpInfo> spInfos = spInfoService.getSpInfoByName(new SpInfo());
        for (SpInfo spInfo : spInfos) {
            Object[] rowData = {spInfo.getId(), spInfo.getName(), spInfo.getPrice(), spInfo.getSellPrice()};
            dtm.addRow(rowData);
        }
    }

    private void button1(ActionEvent e) {
        String name = textField1.getText();
        SpInfo spInfo = new SpInfo(name);
        ArrayList<SpInfo> spInfos = spInfoService.getSpInfoByName(spInfo);
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        if (spInfos.size() > 0) {
            for (SpInfo spInfo1 : spInfos) {
                Object[] rowData = {spInfo1.getId(), spInfo1.getName(), spInfo1.getPrice(), spInfo1.getSellPrice()};
                dtm.addRow(rowData);
            }
        } else {
            JOptionPane.showMessageDialog(null, "没有找到该商品");
        }
    }

    private void table1MouseClicked(MouseEvent e) {
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtm.getValueAt(row, 0);
        String name = (String) dtm.getValueAt(row, 1);
        BigDecimal price = (BigDecimal) dtm.getValueAt(row, 2);
        BigDecimal sellPrice = (BigDecimal) dtm.getValueAt(row, 3);
        textField2.setText(name);
        textField3.setText(String.valueOf(price));
        textField4.setText(String.valueOf(sellPrice));
    }

    private void button2(ActionEvent e) {
        String name = textField2.getText();
        String price = textField3.getText();
        String sellPrice = textField4.getText();
        if (StringUtil.isEmpty(name)) {
            JOptionPane.showMessageDialog(null, "商品名称不能为空");
            return;
        }
        if (StringUtil.isEmpty(price)) {
            JOptionPane.showMessageDialog(null, "商品进价不能为空");
            return;
        }
        if (StringUtil.isEmpty(sellPrice)) {
            JOptionPane.showMessageDialog(null, "商品售价不能为空");
            return;
        }
        BigDecimal price1 = new BigDecimal(price);
        BigDecimal sellPrice1 = new BigDecimal(sellPrice);
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtm.getValueAt(row, 0);
        SpInfo spInfo = new SpInfo(chooseId, name, price1, sellPrice1);
        Boolean aBoolean = spInfoService.updateSpInfo(spInfo);
        if (aBoolean) {
            JOptionPane.showMessageDialog(null, "修改成功");
            fillTable();
        } else {
            JOptionPane.showMessageDialog(null, "修改失败");
        }
    }

    private void button3(ActionEvent e) {
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int id = (int) dtm.getValueAt(row, 0);
        Boolean isDeleteSpInfo = spInfoService.deleteSpInfo(id);
        if (isDeleteSpInfo) {
            JOptionPane.showMessageDialog(null, "删除成功");
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(null, "删除失败");
        }
    }

    private void initComponents() {
        JLabel label1 = new JLabel();
        JTextField textField1 = new JTextField();
        JButton button1 = new JButton();
        JScrollPane scrollPane1 = new JScrollPane();
        JTable table1 = new JTable();
        JLabel label2 = new JLabel();
        JLabel label3 = new JLabel();
        JLabel label4 = new JLabel();
        JTextField textField2 = new JTextField();
        JTextField textField3 = new JTextField();
        JTextField textField4 = new JTextField();
        JButton button2 = new JButton();
        JButton button3 = new JButton();

        setTitle("\u7ba1\u7406\u5546\u54c1");
        label1.setText("\u5546\u54c1\u540d\u5b57");
        button1.setText("\u67e5\u770b");
        button1.addActionListener(e -> button1(e));
        table1.setModel(new DefaultTableModel(
                new Object[][]{
                        {null, null, null, null},
                        {null, null, null, null}
                },
                new String[]{
                        "\u7f16\u53f7", "\u5546\u54c1\u540d\u5b57", "\u8fdb\u4ef7", "\u552e\u4ef7"
                }
        ));
        table1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                table1MouseClicked(e);
            }
        });
        scrollPane1.setViewportView(table1);
        label2.setText("\u5546\u54c1\u540d\u5b57");
        label3.setText("\u5546\u54c1\u8fdb\u4ef7");
        label4.setText("\u5546\u54c1\u552e\u4ef7");
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> button2(e));
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> button3(e));

        GroupLayout contentPaneLayout = new GroupLayout(getContentPane());
        getContentPane().setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(44, 44, 44)
                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addComponent(label1, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(56, 56, 56)
                                                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(180, 180, 180)
                                                                .addComponent(button1))
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                                                        .addComponent(label2, GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                                                                                        .addComponent(label3, GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
                                                                                .addGap(18, 18, 18)
                                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                                                        .addComponent(textField2, GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                                                                                        .addComponent(textField3, GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)))
                                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                                .addComponent(label4, GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                                                                                .addGap(18, 18, 18)
                                                                                .addComponent(textField4, GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)))
                                                                .addGap(18, 18, 18)
                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                                        .addComponent(button2)
                                                                        .addComponent(button3))))
                                                .addGroup(contentPaneLayout.createSequentialGroup()
                                                        .addGap(160, 160, 160)
                                                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 492, GroupLayout.PREFERRED_SIZE)))
                                        .addContainerGap(30, Short.MAX_VALUE))
                        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label1)
                                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button1))
                                .addGap(34, 34, 34)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 241, GroupLayout.PREFERRED_SIZE)
                                .addGap(22, 22, 22)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label2)
                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button2))
                                .addGap(18, 18, 18)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label3)
                                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(button3))
                                .addGap(26, 26, 26)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label4)
                                        .addComponent(textField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                                .addComponent(button3)
                                .addGap(45, 45, 45))
        );

        pack();
        setLocationRelativeTo(getOwner());
    }

    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JButton button2;
    private JButton button3;
}